"""
产品价格数据访问层
"""
from typing import List, Union
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, and_, desc, update
from sqlalchemy.orm import selectinload

from module_gen.constants.gen_constants import GenConstants
from module_product.entity.do.bg_product_price_do import BgProductPrice
from module_product.entity.vo.bg_product_price_vo import BgProductPricePageModel, BgProductPriceModel
from utils.page_util import PageResponseModel, PageUtil


class BgProductPriceDao:
    """产品价格数据访问对象"""

    @classmethod
    async def get_bg_product_price_list(cls, db: AsyncSession,
                                      query_object: BgProductPricePageModel,
                                      data_scope_sql: str = None,
                                      is_page: bool = True) -> Union[List, PageResponseModel]:
        """
        获取产品价格列表
        """
        query = (
            select(BgProductPrice)
            .where(
                BgProductPrice.product_id == query_object.product_id if query_object.product_id else True,
                BgProductPrice.random_price == query_object.random_price if query_object.random_price else True,
                BgProductPrice.del_flag == '0',
                eval(data_scope_sql) if data_scope_sql else True,
            )
            .order_by(desc(BgProductPrice.create_time))
        )
        
        bg_product_price_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return bg_product_price_list

    @classmethod
    async def get_bg_product_price_detail(cls, db: AsyncSession, bg_product_price_id: int) -> BgProductPrice:
        """
        获取产品价格详情
        """
        query = (
            select(BgProductPrice)
            .where(
                BgProductPrice.id == bg_product_price_id,
                BgProductPrice.del_flag == '0'
            )
        )
        result = await db.execute(query)
        return result.scalars().first()

    @classmethod
    async def get_bg_product_prices_by_product_id(cls, db: AsyncSession, product_id: int) -> List[BgProductPrice]:
        """
        根据产品ID获取所有价格
        """
        query = (
            select(BgProductPrice)
            .where(
                BgProductPrice.product_id == product_id,
                BgProductPrice.del_flag == '0'
            )
            .order_by(BgProductPrice.create_time)
        )
        result = (await db.execute(query)).scalars().all()
        return list(result)

    @classmethod
    async def add_bg_product_price(cls, db: AsyncSession, price_model: BgProductPriceModel) -> BgProductPrice:
        """
        添加产品价格
        """
        bg_product_price = BgProductPrice(**price_model.model_dump(exclude_unset=True, ))
        db.add(bg_product_price)
        await db.commit()
        await db.refresh(bg_product_price)
        return bg_product_price

    @classmethod
    async def edit_bg_product_price(cls, db: AsyncSession, edit_model: BgProductPriceModel) -> None:
        """
        编辑产品价格
        """
        edit_dict_data = edit_model.model_dump(exclude_unset=True, exclude={*GenConstants.DAO_COLUMN_NOT_EDIT})
        await db.execute(update(BgProductPrice), [edit_dict_data])
        await db.commit()
        return None

    @classmethod
    async def delete_bg_product_price(cls, db: AsyncSession, bg_product_price_id: int) -> int:
        """
        删除产品价格
        """
        query = (
            select(BgProductPrice)
            .where(
                BgProductPrice.id == bg_product_price_id,
                BgProductPrice.del_flag == '0'
            )
        )
        result = await db.execute(query)
        bg_product_price = result.scalars().first()
        
        if bg_product_price:
            bg_product_price.del_flag = '2'
            await db.commit()
            return 1
        return 0
